-- This is an attempt to prevent the notorious pixel bug.
-- We calculate the scaled value in pixels, then round it to make sure
-- the graphics engine is able to display it correctly, after which we
-- apply the scale again. At least for my scale/resolution this seems to fix
-- most of the issues.
local function ApplyScale(value)
	local scale = UIParent:GetEffectiveScale()
	return floor(value*scale+0.5)/scale
end

local CreateEditBox
do
	local editbox = {}
	local id = 1
	
	-- Apply uiScale to dimensions to hopefuly get rid of pixel bug.
	local width = ApplyScale(256)
	local height = ApplyScale(16)
	
	local function OnEnter(self)
		if self.help then
			GameTooltip:SetOwner(frame, "ANCHOR_RIGHT")
			GameTooltip:AddLine(self.help)
			GameTooltip:Show()
		end
	end

	local function OnLeave(self)
		GameTooltip:ClearLines()
		GameTooltip:Hide()
	end
	
	local function OnTabPressed(self)
		-- Clear focus on me.
		self:ClearFocus()
		
		-- Set focus on next editbox, or the first if this is the last.
		editbox[self.id + 1 > #editbox and 1 or self.id + 1]
	end
	
	function CreateEditBox(parent)
		local box = CreateFrame("EditBox", nil, parent)
		box:SetFontObject("GameFontNormalSmall")
		box:SetTextColor(1,1,1)
		box:SetHeight(16)
		box:SetWidth(256)
		box:SetAutoFocus(false)
		
		local left = box:CreateTexture(nil, "ARTWORK")
		left:SetPoint("TOPLEFT", box, "TOPLEFT", -1, 1)
		left:SetPoint("BOTTOMRIGHT", box, "BOTTOMLEFT", 0, -1)
		left:SetTexture(0,0,0)
			
		local right = box:CreateTexture(nil, "ARTWORK")
		right:SetPoint("TOPLEFT", box, "TOPRIGHT", 0, 1)
		right:SetPoint("BOTTOMRIGHT", box, "BOTTOMRIGHT", 1, -1)
		right:SetTexture(0,0,0)
			
		local bottom = box:CreateTexture(nil, "ARTWORK")
		bottom:SetPoint("TOPLEFT", box, "BOTTOMLEFT")
		bottom:SetPoint("BOTTOMRIGHT", box, "BOTTOMRIGHT", 0, -1)
		bottom:SetTexture(0,0,0)
		
		local top = box:CreateTexture(nil, "ARTWORK")
		top:SetPoint("TOPLEFT", box, "TOPLEFT", 0, 1)
		top:SetPoint("BOTTOMRIGHT", box, "TOPRIGHT")
		top:SetTexture(0,0,0)
		  
		local bg = box:CreateTexture(nil, "BACKGROUND")
		bg:SetAllPoints()
		bg:SetTexture(0.5,0.5,0.5, 0.5)
		
		local title = box:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
		title:SetPoint("TOPLEFT", 0, 12)
		box.title = title
		
		-- Scripts
		box:SetScript("OnEscapePressed", box.ClearFocus)
		box:SetScript("OnEnterPressed", box.ClearFocus)
		box:SetScript("OnEnter", OnEnter)
		box:SetScript("OnLeave", OnLeave)
		box:SetScript("OnTabPrssed", OnTabPressed)
		
		box.id = id
		editbox[id] = box
		id = id + 1
		return box
	end
end

width = floor(oldwidth*scale)/scale